function ber=homework2(power,noise,correl1,correl2,antennas,training)
%function ber=homework2(power,noise,correl1,correl2,antennas,training)
%sphere detection has not been authenticated, could use maximum likelihood detector for lower n
%training process has not been authenticated, no apparent link between training symbols and accuracy

if(nargin<1)
power=100000;	%total power being transmitted
noise=1;	%noise at each receiver
correl1=.6;
correl2=.6;
antennas=[2];
training=[0];
%training=[0 2 4 16 64];

end


largenumber=1;
rho=power/noise;
sample_words={'Hello World' 'whats up doc' 'gone fishin' '867-5309 Jenny' 'Twilight zone' 'random word2007' 'myemail@address.com'}; 

ber=zeros(length(antennas),length(training),4,2);

for a=1:length(antennas)
	[s,byte_sequence]=transmit_qpsk_gray(word2bin(char(sample_words(ceil(rand()*length(sample_words))))),antennas(a));
	h_corr=correlated_channel(antennas(a),antennas(a),correl1,correl2);
	h_uncorr=correlated_channel(antennas(a),antennas(a),0,0);

	for b=1:length(training)

		%detection methods
		for c=1:4
			%dont use this large number, drive this program from an external file
			for d=1:largenumber
				v=noise*randn(antennas(a),1) + j*noise*randn(antennas(a),1);
				singular=1;
				while(singular==1)
					[h_corr_hat,sing1]=channel_estimation(h_corr,noise,rho,antennas(a),antennas(a),training(b),v);
					[h_uncorr_hat,sing2]=channel_estimation(h_uncorr,noise,rho,antennas(a),antennas(a),training(b),v);
					if(sing1==0 & sing2==0)
						singular=0;
					end
					if(singular==1)
						h_corr=correlated_channel(antennas(a),antennas(a),correl1,correl2);
						h_uncorr=correlated_channel(antennas(a),antennas(a),0,0);
					end
				end
	
				ber_corr(d)=blast_accuracy(byte_sequence,blast_detection(c,h_corr,h_corr_hat,s,noise,v,rho,antennas(a)));
				ber_uncorr(d)=blast_accuracy(byte_sequence,blast_detection(c,h_uncorr,h_uncorr_hat,s,noise,v,rho,antennas(a)));
			end
			ber(a,b,c,1)=mean(ber_corr);
			ber(a,b,c,2)=mean(ber_uncorr);
		end
	end
end

correlated_mean=mean(mean(mean(ber(:,:,:,1))))
uncorrelated_mean=mean(mean(mean(ber(:,:,:,2))))
%sphere_detect=mean(mean(mean(ber(:,:,1,:))))
%simple_cancel=mean(mean(mean(ber(:,:,2,:))))
%zf_cancel_ordering=mean(mean(mean(ber(:,:,3,:))))
%mmse_nulling=mean(mean(mean(ber(:,:,4,:))))
%notraining=mean(mean(mean(ber(:,1,:,:))))
%training1=mean(mean(mean(ber(:,2,:,:))))
%training2=mean(mean(mean(ber(:,3,:,:))))


